<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/11/13 14:47
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\Api\V1\App\Common;


use App\Http\Controllers\ApiController;
use App\Models\Currency;
use App\Models\Language;
use App\Models\Setting;
use App\Models\Store;
use App\Services\Utils\ImageHelper;


class ResourceController extends ApiController
{
    public function create()
    {
        $languages = Language::allStatusEnable();
        $currency = Currency::allCurrency();
        $stores = $this->getStores();

        return $this->response(compact('languages', 'currency', 'stores'));
    }

    /**
     * @return Store[]|\Illuminate\Database\Eloquent\Collection
     */
    private function getStores()
    {
        $stores = Store::all()
            ->prepend(Store::default())
            ->transform(function (Store $store) {
                $setting_configs = Setting::findConfigByCode('config', $store->getKey());
                $store->config = collect($setting_configs)
                    // config 不要全部返回，可能包含密码
                    ->only([
                        'config_name', 'config_telephone', 'config_email',
                        'config_address', 'config_language', 'config_currency' ,
                        Setting::KEY_CONFIG_APP_SPLASH_IMAGE,
                        Setting::KEY_CONFIG_APP_THEME,
                    ])
                    ->transform(function ($value, $key) {
                        // app splash image path to url
                        if ($key === Setting::KEY_CONFIG_APP_SPLASH_IMAGE) {
                            return ImageHelper::toOpenCartUrl($value);
                        }
                        return $value;
                    })
                ;

                return $store;
            });

        return $stores;
    }
}